<template>
    <el-dialog
            :title="!dataForm.customerId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="企业客户唯一标识" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="企业客户唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="企业名称" prop="enterpriseName">
                        <el-input v-model="dataForm.enterpriseName" placeholder="企业名称"></el-input>
                </el-form-item>
                <el-form-item label="统一社会信用代码" prop="creditCode">
                        <el-input v-model="dataForm.creditCode" placeholder="统一社会信用代码"></el-input>
                </el-form-item>
                <el-form-item label="企业类型" prop="enterpriseType">
                        <el-input v-model="dataForm.enterpriseType" placeholder="企业类型"></el-input>
                </el-form-item>
                <el-form-item label="注册地址" prop="registeredAddress">
                        <el-input v-model="dataForm.registeredAddress" placeholder="注册地址"></el-input>
                </el-form-item>
                <el-form-item label="法定代表人" prop="legalRepresentative">
                        <el-input v-model="dataForm.legalRepresentative" placeholder="法定代表人"></el-input>
                </el-form-item>
                <el-form-item label="注册资本" prop="registeredCapital">
                        <el-input v-model="dataForm.registeredCapital" placeholder="注册资本"></el-input>
                </el-form-item>
                <el-form-item label="成立时间" prop="establishmentDate">
                        <el-date-picker
                                v-model="dataForm.establishmentDate"
                                type="datetime"
                                placeholder="成立时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="经营范围" prop="businessScope">
                        <el-input v-model="dataForm.businessScope" placeholder="经营范围"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            customerId: null,
            enterpriseName: null,
            creditCode: null,
            enterpriseType: null,
            registeredAddress: null,
            legalRepresentative: null,
            registeredCapital: null,
            establishmentDate: null,
            createdAt: null,
            updatedAt: null,
            businessScope: null
          },
          dataRule: {
            customerId: [
                        { required: true, message: '企业客户唯一标识不能为空', trigger: 'blur' }
            ],
            enterpriseName: [
                        { required: true, message: '企业名称不能为空', trigger: 'blur' }
            ],
            creditCode: [
                        { required: true, message: '统一社会信用代码不能为空', trigger: 'blur' }
            ],
            enterpriseType: [
                        { required: true, message: '企业类型不能为空', trigger: 'blur' }
            ],
            registeredAddress: [
                        { required: true, message: '注册地址不能为空', trigger: 'blur' }
            ],
            legalRepresentative: [
                        { required: true, message: '法定代表人不能为空', trigger: 'blur' }
            ],
            registeredCapital: [
                        { required: true, message: '注册资本不能为空', trigger: 'blur' }
            ],
            establishmentDate: [
                        { required: true, message: '成立时间不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ],
            businessScope: [
                        { required: true, message: '经营范围不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.customerId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.customerId = id || 0
            if (this.dataForm.customerId) {
              this.$http({
                url: this.$http.adornUrl(`/app/enterprise_customer/info/${this.dataForm.customerId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appenterprisecustomer
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/enterprise_customer/${!this.dataForm.customerId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>